package com.ht.controller;

import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.annotation.JsonAlias;
import com.ht.bean.*;
import com.ht.common.FileUpload;
import com.ht.common.Filedownload;
import com.ht.service.EnrollmentService;
import com.mysql.cj.Session;
import org.junit.runner.Request;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.w3c.dom.stylesheets.MediaList;

import javax.annotation.Resource;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.*;
import java.net.URLEncoder;
import java.util.*;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.net.URLEncoder;
import java.io.InputStream;

@Controller
@RequestMapping("/et")
public class EnrollmentController {

    @Resource
    private EnrollmentService is;


    //去查看招生列表  and 专业表
    @RequestMapping("/list")
    public String List(Model model) {
        List<Major> list = is.allmajor();
        List<Classtype> list9 = is.allclasstype();
        model.addAttribute("majorList", list);
        model.addAttribute("classtypelist",list9);
        return "etlist";
    }

    //查询招生表
    @RequestMapping("/etlist")
    @ResponseBody
    public Map list(Integer page, Integer limit, Enrollment et) {
        System.out.println(page + "" + limit);
        List<Map> list = is.etlist((page - 1) * limit, limit, et);
        Map map = new HashMap();
        map.put("code", 0);
        map.put("msg", "成功");
        map.put("count", is.countet(et));//总页数
        map.put("data", list);//数据
        return map;
    }

    //新增
    @RequestMapping("/addenrllem")
    @ResponseBody
    public Map adddkjkjj(Enrollment enrollment) {
        //自动获取时间
//        enrollment.setTestTime(new Date().toLocaleString());
        enrollment.setStartTime(new Date().toLocaleString());
        enrollment.setSigndate(new Date().toLocaleString());
        enrollment.setPaymentTime(new Date().toLocaleString());
        enrollment.setEnrollMoneyTime(new Date().toLocaleString());
        enrollment.setReviewerTime(new Date().toLocaleString());
        //预定报名费默认为0
        enrollment.setAmount("0");
        //预定报名费审核默认为未审核
        enrollment.setReviewer("1");
        //提成金额
        enrollment.setEnrollMoney("0");

        System.out.println("dqw1" + enrollment);


        Map map = new HashMap();
        boolean bln = is.etadd(enrollment);
        if (bln) {
            map.put("msg", "添加成功");
        } else {
            map.put("msg", "添加失败!");
        }
        System.out.println("数据" + map);
        return map;
    }

    //去新增
    @RequestMapping("/toadd")
    public String toadd(Model model) {
        //查专业表
        List<Major> mlist = is.allmajor();
        List<Classtype> clist = is.allclasstype();
        model.addAttribute("mlist", mlist);
        model.addAttribute("clist",clist);

        return "etadd";
    }

    //删除
    @RequestMapping("/del")
    @ResponseBody
    public Map dela(String enrollmentId) {
        Map map = new HashMap();
        boolean bln = is.delet(enrollmentId);
        if (bln) {
            map.put("msg", "删除成功");
        } else {
            map.put("msg", "删除失败");
        }
        return map;
    }

    //预定报名费
    @RequestMapping("ydbmf/{enrollmentId}")
    public String ydbmf(@PathVariable("enrollmentId")Integer enrollmentId ,Model model){
        Enrollment enrollmentx = is.cxid(enrollmentId);
        model.addAttribute("enrollmentId",enrollmentId);
        model.addAttribute("enrollmentId1",enrollmentx);
        return "etydbmf";
    }

    @RequestMapping("/ydbmfupd")
    @ResponseBody
    public Map ydbmfupd(Enrollment enrollment) {
        Map map = new HashMap();
        if (is.ydbmf(enrollment)) {
            map.put("msg", "修改成功");
        } else {
            map.put("msg", "修改失败!");
        }
        return map;
    }

    //预定报名费审核
    @RequestMapping("ydbmfsh/{enrollmentId}")
    public String ydbmfsh(@PathVariable("enrollmentId")Integer enrollmentId ,Model model){
        Enrollment enrollmentx = is.cxid(enrollmentId);
        model.addAttribute("enrollmentId",enrollmentId);
        model.addAttribute("enrollmentId13",enrollmentx);
        return "etydbmfsh";
    }

    @RequestMapping("/ydbmfshupd")
    @ResponseBody
    public Map ydbmfshupd(Enrollment enrollment) {
        Map map = new HashMap();
        if (is.ydbmfsh(enrollment)) {
            map.put("msg", "修改成功");
        } else {
            map.put("msg", "修改失败!");
        }
        return map;
    }

    //提成金额
    @RequestMapping("tcje/{enrollmentId}")
    public String tcje(@PathVariable("enrollmentId")Integer enrollmentId ,Model model){
        Enrollment enrollmentx = is.cxid(enrollmentId);
        model.addAttribute("enrollmentId",enrollmentId);
        model.addAttribute("enrollmentId12",enrollmentx);
        return "ettcje";
    }

    @RequestMapping("/tcjea")
    @ResponseBody
    public Map tcje1(Enrollment enrollment) {
        Map map = new HashMap();
        if (is.tcje(enrollment)) {
            map.put("msg", "修改成功");
        } else {
            map.put("msg", "修改失败!");
        }
        return map;
    }

    //试学
    @RequestMapping("sx/{enrollmentId}")
    public String sx(@PathVariable("enrollmentId")Integer enrollmentId ,Model model){
        Enrollment enrollmentx = is.cxid(enrollmentId);
        model.addAttribute("enrollmentId",enrollmentId);
        model.addAttribute("enrollmentId11",enrollmentx);
        return "etsx";
    }

    @RequestMapping("/sxsj")
    @ResponseBody
    public Map sxsj(Enrollment enrollment) {
        Map map = new HashMap();
        if (is.sx(enrollment)) {
            map.put("msg", "修改成功");
        } else {
            map.put("msg", "修改失败!");
        }
        return map;
    }

    //学生状态  未试学/流失
    @RequestMapping("wsx/{enrollmentId}")
    public String wsx(@PathVariable("enrollmentId")Integer enrollmentId ,Model model){
        Enrollment enrollmentx = is.cxid(enrollmentId);
        model.addAttribute("enrollmentId",enrollmentId);
        model.addAttribute("enrollmentId10",enrollmentx);
        return "etwsx";
    }


    @RequestMapping("/wsxa")
    @ResponseBody
    public Map wsxa(Enrollment enrollment) {
        Map map = new HashMap();
        if (is.wsx(enrollment)) {
            map.put("msg", "修改成功");
        } else {
            map.put("msg", "修改失败!");
        }
        return map;
    }

    //修改
    @RequestMapping("/etupd/{enrollmentId}")
    public String toupd(@PathVariable("enrollmentId") String enrollmentId, Model model) {
        //查专业表
        List<Major> mlist = is.allmajor();
        List<Classtype> clist = is.allclasstype();
        model.addAttribute("mlist", mlist);
        model.addAttribute("clist",clist);
        //查询要修改的id
        List<Enrollment> enrollments = is.selbyid(enrollmentId);
        model.addAttribute("enrollmentId", enrollmentId);
        model.addAttribute("enrollment", JSON.toJSON(enrollments.get(0)));//将对象转换成JSON对象
        return "etupd";
    }

    //去修改
    @RequestMapping("/toupd")
    @ResponseBody
    public Map upd(Enrollment enrollment) {
        Map map = new HashMap();
        if (is.updet(enrollment)) {
            map.put("msg", "修改成功");
        } else {
            map.put("msg", "修改失败!");
        }
        return map;
    }

    //-----------------------------------------------------------
    //文件管理模块
    @RequestMapping("/datadoclist")
    public String wj(Model model) {
        List<Emp> list = is.allemp();
        model.addAttribute("empList", list);
        return "dclist";
    }

    //查询总分页
    @RequestMapping("/listdc")
    @ResponseBody
    public Map list(Integer page, Integer limit, Datadoc datadoc) {
        System.out.println(page + "" + limit);
        List<Map> listx = is.alldc((page - 1) * limit, limit, datadoc);
        Map map = new HashMap();
        map.put("code", 0);
        map.put("msg", "成功");
        map.put("count", is.counte(datadoc));//总页数
        map.put("data", listx);//数据
        return map;
    }

    //新增
    @RequestMapping("/dcadd")
    @ResponseBody
    public Map adddatadoc(Datadoc datadoc, MultipartFile fileface, HttpSession session) {
        if (fileface.getOriginalFilename() == null || fileface.getOriginalFilename().equals("")) {
            System.out.println("文件为空");
            return null;
        }

        // 截取文件的扩展名(如.jpg)
        String oriName = fileface.getOriginalFilename();  //获取上传时的文件名
        System.out.println("..................+++++++:" + oriName);

        String filePath = FileUpload.upload(fileface, session, "face");
        if (filePath != null) {
            datadoc.setDocName(oriName);
            datadoc.setUrl(filePath);
        }

        datadoc.setOptime(new Date().toString());
        System.out.println("dqw1" + datadoc);


        Map map = new HashMap();
        boolean bln = is.dcadd(datadoc);
        if (bln) {
            map.put("msg", "添加成功");
        } else {
            map.put("msg", "添加失败!");
        }
        System.out.println("数据" + map);
        return map;
    }

    //删除
    @RequestMapping("/dcdel")
    @ResponseBody
    public Map dcdel(String docId) {
        Map map = new HashMap();
        boolean bln = is.dcdel(docId);
        if (bln) {
            map.put("msg", "删除成功");
        } else {
            map.put("msg", "删除失败");
        }
        return map;
    }

    //下载文件
    @RequestMapping(value = "/dclist",method = RequestMethod.GET)
    public void download(HttpServletResponse response,String docId,HttpSession session) {
        Datadoc a = is.XzDatadoc(docId);
        String path = session.getServletContext().getRealPath("\\")+a.getUrl(); //拿到文件路径
        try {
            // path是指欲下载的文件的路径。
            File file = new File(path);
            // 取得文件名。
            String filename = a.getDocName(); //file.getName;
            // 取得文件的后缀名。
            String ext = filename.substring(filename.lastIndexOf(".") + 1).toUpperCase();
            // 以流的形式下载文件。
            InputStream fis = new BufferedInputStream(new FileInputStream(path));
            byte[] buffer = new byte[fis.available()];
            fis.read(buffer);
            fis.close();
            // 清空response
            response.reset();
            // 设置response的Header
            response.addHeader("Content-Disposition", "attachment;filename=" + new String(filename.getBytes()));
            response.addHeader("Content-Length", "" + file.length());
            OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
            response.setContentType("application/octet-stream");
            toClient.write(buffer);
            toClient.flush();
            toClient.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }


    //--------------------------------------------------------------------------------------------------------------------
    //系统报表
    //招生统计
    //去查看招生列表  and 专业表 and 班级分类
    @RequestMapping("/tjlist")
    public String tjlist(Model model) {
        List<Major> list = is.allmajor();
        List<Classtype> list9 = is.allclasstype();
        model.addAttribute("majorList", list);
        model.addAttribute("classtypelist",list9);
        return "zhaoshengtj";
    }

    //查询招生表
    @RequestMapping("/bblist")
    @ResponseBody
    public Map bblist(Integer page, Integer limit, Enrollment et) {
        System.out.println(page + "" + limit);
        List<Map> list = is.bblist((page - 1) * limit, limit, et);
        Map map = new HashMap();
        map.put("code", 0);
        map.put("msg", "成功");
        map.put("count", is.countb(et));//总页数
        map.put("data", list);//数据
        return map;
    }

    //查询招生列表  整图形
    @RequestMapping("/zstx")
    public String zstx(){

        return "zhaoshengtj";
    }

    //去查询招生列表
    @RequestMapping("/qzstx")
    @ResponseBody
    public Map line(){
        List<Map> list0 = is.txlist();
        List<String> list = new ArrayList<>();
        List<Integer> list1 = new ArrayList<>();
        for (Map studentes : list0){
            list.add((String) studentes.get("negativeName"));
            list1.add(Integer.parseInt(studentes.get("ren").toString()));
        }
        System.out.println("jksaghdjas"+list+list1);
        Map map=new HashMap();
        map.put("month",list);//数据
        map.put("amount1",list1);//数据
        return map;
    }



    //预定报名费统计
    //查询招生表
    @RequestMapping("/ydtjlist")
    @ResponseBody
    public Map ydtjlist(Integer page, Integer limit, Enrollment et) {

        System.out.println(page + "" + limit);
        List<Map> list = is.fylist((page - 1) * limit, limit, et);
        Map map = new HashMap();
        map.put("code", 0);
        map.put("msg", "成功");
        map.put("count", is.countf(et));//总页数
        map.put("data", list);//数据
        return map;
    }


    //去查看招生列表
    @RequestMapping("/mflist")
    public String mflist() {

        return "yubaomingtj";
    }

}
